package com;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileOutputStream;
import java.io.IOException;

public class Sjlx2Update {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse("src/com/手机信息.xml");
        NodeList list=doc.getElementsByTagName("Brand");
        for (int i = 0; i <list.getLength() ; i++) {
            Element brandElement=(Element) list.item(i);
            String brandName=brandElement.getAttribute("name");
            if(brandName.equals("三星")){
                brandElement.setAttribute("name","SAMSUNG");
            }
        }
        TransformerFactory transformerFactory=TransformerFactory.newInstance();
        Transformer transform= transformerFactory.newTransformer();
        DOMSource domSource=new DOMSource(doc);

        transform.setOutputProperty(OutputKeys.ENCODING,"utf-8");
        StreamResult result=new StreamResult(new FileOutputStream("src/com/手机信息.xml"));
        transform.transform(domSource,result);
    }
}
